<?php
/**
*btc列表
*/

require_once 'common.inc.php';
//!in_array('2',$larr)&&adminmsg('你没有被赋予操作该项的权限！');

$matches=parse_url($_SERVER['HTTP_REFERER']);
//$url=$matches['scheme'].'://'.$matches['host'].'/';

$btcarray=array('maxmemory' , 'sendtcpcushion',  'accepttcpcushion' , 'maxconnect' , 'minconnect' ,'maxminpoint' , 'minmaxpoint' , 'maxupcs' , 'tspeed' , 'dspeed' ,'telltype',  'warnlevel' , 'udradio' , 'btcip' , 'traip' , 'port' , 'ifstoport',  'ifudptra'  ,'ifhttps',  'ifmd5' , 'comsize' , 'comtime' , 'openfiles'  ,'psocks' , 'ifsplittime',  'ifcam'  ,'ifusesur' , 'ipscope',  'ifallowdown' ,'btcopen','btcname','maxpoint','minpoint','maxpeer','minpeer');
$order_arr=array('maxmemory'=>array(6,1,0,0,33) , 'sendtcpcushion'=>array(6,1,0,0,34),  'accepttcpcushion'=>array(6,1,0,0,34) , 'maxconnect'=>array(6,1,0,0,35) , 'minconnect'=>array(6,1,0,0,35) ,'maxminpoint' =>array(6,1,0,0,36), 'minmaxpoint'=>array(6,1,0,0,37) , 'maxupcs'=>array(6,1,0,0,38) , 'tspeed'=>array(6,1,0,0,39) , 'dspeed'=>array(6,1,0,0,39) ,'telltype'=>array(6,1,0,0,40),  'warnlevel' =>array(6,1,0,0,41), 'udradio' =>array(6,1,0,0,42), 'btcip'=>array(6,1,0,0,43) , 'traip' =>array(6,1,0,0,44), 'port' =>array(6,1,0,0,45), 'ifstoport'=>array(6,1,0,0,46),  'ifudptra' =>array(6,1,0,0,47) ,'ifhttps'=>array(6,1,0,0,48),  'ifmd5'=>array(6,1,0,0,49) , 'comsize'=>array(6,1,0,0,50) , 'comtime'=>array(6,1,0,0,51) , 'openfiles' =>array(6,1,0,0,52) ,'psocks'=>array(6,1,0,0,53) , 'ifsplittime'=>array(6,1,0,0,54),  'ifcam'=>array(6,1,0,0,55)  ,'ifusesur'=>array(6,1,0,0,56) , 'ipscope'=>array(6,1,0,0,57),  'ifallowdown'=>array(6,1,0,0,58),
'btcopen'=>array(6,1,0,0,59));

$reg_ill=array('maxmemory'=>array(0,10) , 'sendtcpcushion'=>array(0,10),  'accepttcpcushion'=>array(-10,0) , 'maxconnect'=>array(0,10) , 'minconnect'=>array(0,10) ,'maxminpoint' =>array(10,-10), 'minmaxpoint'=>array(-10,10) , 'maxupcs'=>array(-10,10) , 'tspeed'=>array(-10,10) , 'dspeed'=>array(-10,10) , 'warnlevel' =>array(0,10), 'udradio' =>array(0,10),'comsize'=>array(0,10) , 'comtime'=>array(0,60) , 'openfiles' =>array(0,20) ,'psocks'=>array(0,10));

if('btclist'==$action)
{
	$limit=10;
	$page=intval($page)<1?1:intval($page);
	$start=(intval($page)-1)*$limit;
	$sql="select count(*) from area group by `describe`";
	$query=$dsid->query($sql);
	$count=$dsid->fetch_array($query);
	//print $count[0];
	$allpage=ceil($count['count(*)']/$limit);
	$page=$page>$allpage?$allpage:$page;
	$url='btc.inc.php?action='.$action.'&amp;';
	$pages=numofpage($count['count(*)'],$page,$allpage,$url);
	
	
	$btclist=array();

	//有问题
	$sql="select * from area group by `describe` limit $start,$limit";
	
	

	$query=$dsid->query($sql);
	while($btc=$dsid->fetch_array($query))
	{
		if($btc['describe'])
		{
			$cactisql = "SELECT g.id FROM  graph_tree_items g LEFT JOIN host h ON g.host_id=h.id WHERE h.hostname='$btc[describe]'";
			$cactiquery = $cacti->query($cactisql);
			$leaf_id = $cacti->result($cactiquery,0);
			if($leaf_id)
			{
				$btc['leaf_id'] = $leaf_id;
			}
		}
		$dsquery = $dsid->query("select area,btcspacewarn,btcspacedelete from area where `describe` ='$btc[describe]' and port='$btc[port]'");		
		$breakdown = $dsid->result($dsquery,0);	
		1==$breakdown ? $btc['breakdown'] = "images/normal.jpg" : $btc['breakdown'] = "images/breakdown.jpg";
		1==$breakdown ? $btc['alt'] = "ACS缓存系统运行正常" : $btc['alt'] = "ACS缓存系统运行故障";
		//新加开始
		//有故障，就不显示
		if(1!=$breakdown)
			continue;
		//新加结束
		$btclist[]=$btc;
		$warn= $dsid->query("select btcspacewarn,btcspacedelete,freespace from area where `describe`='$btc[describe]'");
		$warnd[]=$dsid->fetch_array($warn);
	}
	include_once MCC_ROOT.'/templates/msystem/btclist.htm';
}
/*elseif('btcadd'==$action)
{
	$action='btcdoadd';
	$btcdb=array('maxmemory'=>'100' , 'sendtcpcushion'=>'1',  'accepttcpcushion' =>'-1', 'maxconnect' =>'101', 'minconnect' =>'1','maxminpoint'=>'10' , 'minmaxpoint' =>'5', 'maxupcs' =>'10', 'tspeed'=>'10' , 'dspeed'=>'-10' ,'telltype'=>'1',  'warnlevel'=>'50' , 'udradio'=>'1' , 'btcip' =>'127.0.0.1', 'traip' =>'127.0.0.1', 'port'=>'80' , 'ifstoport'=>'1',  'ifudptra'  =>'1','ifhttps'=>'1',  'ifmd5' =>'1', 'comsize'=>'20' , 'comtime'=>'1' , 'openfiles'=>'1'  ,'psocks' =>'1', 'ifsplittime'=>'1',  'ifcam'=>'1'  ,'ifusesur' =>'1', 'ipscope'=>'127.0.0.1/10',  'ifallowdown'=>'1' ,'btcopen'=>'1','btcname'=>'CAS','minpeer'=>'-1','maxpeer'=>'1','maxpoint'=>'1','minpoint'=>'-1');
	$ck_ifbtcopen='checked=true';
	$ck_ifstoport='checked=true';
	$ck_ifudptra='checked=true';
	$ck_fifudptra='checked=true';
	$ck_ifhttps='checked=true';
	$ck_ifmd5='checked=true';
	$ck_ifsplittime='checked=true';
	$ck_ifcam='checked=true';
	$ck_ifusesur='checked=true';
	include_once MCC_ROOT.'/templates/msystem/btcadd.htm';
}
elseif('btcdoadd'==$action)
{
	$sql='insert into msys_btc ';
	$first=0;
    foreach($_POST as $key=>$value)
	{
		if($first)
		{
			if(in_array($key,$btcarray))
			{
				$sqlkey.=",$key";
				$sqlvalue.=",'$value'";
			}
		}
		else
		{
		    if(in_array($key,$btcarray))
			{
				$sqlkey=$sql. "( $key ";
				$sqlvalue=" values ('$value' ";
				$first=1;
			}
		}
		if(1==reg_judge($key,$ovalue)&&array_key_exists($key,$order_arr))
		{
			$temppara=$ovalue;
					//print $temppara.'-----';
			$rep=total_order($order_arr[$key][0],$order_arr[$key][1],$order_arr[$key][2],$order_arr[$key][3],$order_arr[$key][4],$temppara,$tempdb['btcip']);
					order_send($rep);	
		}
		else
		{
			adminmsg('参数设置超出范围','btc.inc.php?action=btclist');
		}			
	}	
	$sql=$sqlkey.')'.$sqlvalue.')';
	$db->query($sql);
	$btc_id=$db->insert_id();
	adminmsg('添加成功','btc.inc.php?action=btclist');	
}
elseif('btcdelete'==$action)
{
	$sql="delete from msys_btc where btcid='$btcid'";
	$db->query($sql);

	adminmsg('删除成功','btc.inc.php?action=btclist');	
}
elseif('btcedit'==$action)
{
	$action='btcdoedit';
	if($btcid)
	{
		$btclist=array();
		$sql="select * from msys_btc where btcid='$btcid'";
		$query=$db->query($sql);
		$btcdb=$db->fetch_array($query);
		
		if($btcdb['ifstoport']=='1')
		{
			$ck_ifstoport="checked=true";
		}
		else
		{
			$ck_fifstoport="checked=true";
		}
		if ($btcdb['ifudptra']=='1'){
			$ck_ifudptra="checked=true";
		}
		else
		{
			$ck_fifudptra="checked=true";
		}
		if ($btcdb['ifhttps']=='1'){
			$ck_ifhttps="checked=true";
		}
		else
		{
			$ck_fifhttps="checked=true";
		}
		if ($btcdb['ifmd5']=='1'){
			$ck_ifmd5="checked=true";
		}
		else
		{
		 	$ck_fifmd5="checked=true";
		}
		if ($btcdb['ifsplittime']=='1'){
			$ck_ifsplittime="checked=true";
		}
		else
		{
			$ck_fifsplittime="checked=true";
		}
		if ($btcdb['ifcam']=='1'){
			$ck_ifcam="checked=true";
		}
		else
		{
			$ck_fifcam="checked=true";
		}
		if ($btcdb['ifusesur']=='1'){
			$ck_ifusesur="checked=true";
		}
		else
		{
			$ck_fifusesur="checked=true";
		}
		if($btcdb['btcopen']=='1')
		{
			$ck_ifbtcopen="checked=true";
		}
		else
		{
			$ck_fifbtcopen="checked=true";
		}
	}	
		include_once MCC_ROOT.'/templates/msystem/btclista.htm';
}
elseif('btcdoedit'==$action)
{
	$btcid=intval($btcid);
	if(!$btcip)
		adminmsg('没有设置ACS的IP','btc.inc.php?action=btcedit&code=edit&amp;btcid='.$btcid);
	if($btcid)
	{
		$query=$db->query("select * from area where btcid='$btcid'");
		$tempdb=$db->fetch_array($query);
		
		$sql='';
		foreach($_POST as $key=>$value)
		{
			if(isset($tempdb[$key])&&$value!=$tempdb[$key])
			{
				if(array_key_exists($key,$reg_ill)&&reg_judge($key,$value)===false)
				{
					continue;
				}
				$sql.="$key='$value',";
			}
		}
						
		foreach($_POST as $okey=>$ovalue)
		{
			if(isset($tempdb[$okey])&&$ovalue!=$tempdb[$okey]&&array_key_exists($okey,$order_arr))
			{
				if(1==reg_judge($okey,$ovalue))
				{
					if($okey=='warnlevel')
					{
						$tempwarn=$ovalue;
						$tempip=ip2long($tempdb['btcip']);
						//$mdb->query("update area set btcspacewarn='$tempwarn' where ip='$tempip'");
					}
					$temppara=$ovalue;
					//print $temppara.'-----';
					$rep=total_order($order_arr[$okey][0],$order_arr[$okey][1],$order_arr[$okey][2],$order_arr[$okey][3],$order_arr[$okey][4],$temppara,$_POST['btcip']);
					//$rep=total_order($order_arr[$okey][0],$order_arr[$okey][1],$order_arr[$okey][2],$order_arr[$okey][3],$order_arr[$okey][4],$temppara,$tempdb['btcip']);
					order_send($rep);
				}
				else
				{
					adminmsg('参数设置超出范围','btc.inc.php?action=btcedit&code=edit&amp;btcid='.$btcid);
				}
			}
		}
		
		$sql=substr($sql,0,-1);
		$sql&&$db->query("update msys_btc  set $sql where btcid='$btcid'");
		//print $sql;
	}
	adminmsg('修改成功','btc.inc.php?action=btcedit&code=edit&amp;btcid='.$btcid);
}*/
?>